from ronglian_sms_sdk import SmsSDK
import json

# 容联云通讯分配的主账号ID
accId = '8aaf07087c16b0a3017c1ad4d0a40065'
# 容联云通讯分配的主账号TOKEN
accToken = '1e98898a53824e76a771ef7abe36d6bc'
# 容联云通讯分配的应用ID
appId = '8aaf07087c16b0a3017c1ad4d1bc006c'

class CCP(object):
    """用自己封装的发送短信的辅助类"""
    # 用来保存对象的类属性
    instance = None

    def __new__(cls):
        # 判断CCP类有没有已经创建好的对象，如果没有，创建一个对象，并且保存
        # 如果有，则将保存的对象直接返回
        if cls.instance is None:

            obj = super(CCP, cls).__new__(cls)

            # 初始化SDK
            obj.sdk = SmsSDK(accId, accToken, appId)

            cls.instance = obj
        return cls.instance


    def send_message(self, tid, mobile, datas):
        """
        发送短信函数
        :param tid: 模板
        :param mobile: 手机号
        :param datas: 数据
        :return:
        """
        resp = self.sdk.sendMessage(tid, mobile, datas)

        #print(resp)
        # {"statusCode":"000000","templateSMS":{"smsMessageSid":"ab62dbff122e46928b2eda55461db4b5","dateCreated":"20210925110118"}}
        result = json.loads(resp)
        status_code = result.get("statusCode")
        if status_code == "000000":
            # 表示发送短信成功
            return 0
        else:
            # 表示发生失败
            return -1


if __name__ == '__main__':
    ccp = CCP()

    ccp.send_message(1,"13305***44",["12**"])
